From a4769c8f27ce212f40bc6f681a55a01a949c8c8d Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 11 Mar 2022 13:52:01 -0700 Subject: [PATCH] retire strsub, gstrsub. (#862) --- defs.h | 3 --- explorist_ini.cc | 24 +++++++++--------- util.cc | 66 ------------------------------------------------ 3 files changed, 12 insertions(+), 81 deletions(-) diff --git a/defs.h b/defs.h index b08b45f8a..662ba59d5 100644 --- a/defs.h +++ b/defs.h @@ -1061,9 +1061,6 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n) int str_match(const char* str, const char* match); -char* strsub(const char* s, const char* search, const char* replace); -char* gstrsub(const char* s, const char* search, const char* replace); - void rtrim(char* s); char* lrtrim(char* buff); int xasprintf(char** strp, const char* fmt, ...) PRINTFLIKE(2, 3); diff --git a/explorist_ini.cc b/explorist_ini.cc index 72a9df9c5..e9eafdb35 100644 --- a/explorist_ini.cc +++ b/explorist_ini.cc @@ -30,20 +30,20 @@ explorist_ini_try(const char* path) info->track_path = nullptr; info->waypoint_path = nullptr; - char* s = xstrdup(inifile_readstr(inifile, "UGDS", "WpFolder")); - if (s) { - s = gstrsub(s, "\\", "/"); - xasprintf(&info->waypoint_path, "%s/%s", path, s); + QString s = inifile_readstr(inifile, "UGDS", "WpFolder"); + if (!s.isNull()) { + s.replace('\\', '/'); + xasprintf(&info->waypoint_path, "%s/%s", path, CSTR(s)); } - s = xstrdup(inifile_readstr(inifile, "UGDS", "GcFolder")); - if (s) { - s = gstrsub(s, "\\", "/"); - xasprintf(&info->geo_path, "%s/%s", path, s); + s = inifile_readstr(inifile, "UGDS", "GcFolder"); + if (!s.isNull()) { + s.replace('\\', '/'); + xasprintf(&info->geo_path, "%s/%s", path, CSTR(s)); } - s = xstrdup(inifile_readstr(inifile, "UGDS", "TrkFolder")); - if (s) { - s = gstrsub(s, "\\", "/"); - xasprintf(&info->track_path, "%s/%s", path, s); + s = inifile_readstr(inifile, "UGDS", "TrkFolder"); + if (!s.isNull()) { + s.replace('\\', '/'); + xasprintf(&info->track_path, "%s/%s", path, CSTR(s)); } inifile_done(inifile); diff --git a/util.cc b/util.cc index a329463c7..9007b68e7 100644 --- a/util.cc +++ b/util.cc @@ -935,72 +935,6 @@ double degrees2ddmm(double deg_val) return (deg * 100.0) + ((deg_val - deg) * 60.0); } -/* - * replace a single occurrence of "search" in "s" with "replace". - * Returns an allocated copy if substitution was made, otherwise returns NULL. - * Doesn't try to make an optimally sized dest buffer. - */ -char* -strsub(const char* s, const char* search, const char* replace) -{ - int len = strlen(s); - int slen = strlen(search); - int rlen = strlen(replace); - - const char* p = strstr(s, search); - if (!slen || !p) { - return nullptr; - } - - char* d = (char*) xmalloc(len + rlen + 1); - - /* Copy first part */ - len = p - s; - memcpy(d, s, len); - d[len] = 0; - - /* Copy replacement */ - strcat(d, replace); - - /* Copy last part */ - strcat(d, p + slen); - return d; -} - -/* - * As strsub, but do it globally. - */ -char* -gstrsub(const char* s, const char* search, const char* replace) -{ - int ooffs = 0; - const char* c; - const char* src = s; - int olen = strlen(src); - int slen = strlen(search); - int rlen = strlen(replace); - - char* o = (char*) xmalloc(olen + 1); - - while ((c = strstr(src, search))) { - olen += (rlen - slen); - o = (char*) xrealloc(o, olen + 1); - memcpy(o + ooffs, src, c - src); - ooffs += (c - src); - src = c + slen; - if (rlen) { - memcpy(o + ooffs, replace, rlen); - ooffs += rlen; - } - } - - if (ooffs < olen) { - memcpy(o + ooffs, src, olen - ooffs); - } - o[olen] = '\0'; - return o; -} - /* * */ -- 2.30.2